import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanxiang_app/bean/simple_entity.dart';
import 'package:wanxiang_app/utils/constant.dart';
import '../../bean/login_entity.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../utils/user_util.dart';
import '../../widgets/account_textField2.dart';
import '../../widgets/money_textField.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';

class WithdrawController extends GetxController{
  @override
  void onInit() {
    super.onInit();
    _getInfo();
  }
  @override
  void onClose() {
    super.onClose();
  }
  bool isPsw = false;
  _getInfo() {
    DioManager.getInstance().post(ServiceUrl.myInfo,{
      "user_name":UserUtil.getUSER()?.userName
    }, (data1) async {
      LoginEntity  entity = LoginEntity.fromJson(jsonDecode(data1));
      if(entity.data.withdrawPw.isEmpty){
        isPsw = false;
      }else{
        isPsw = true;
      }
      update();
    }, (error) {
      ToastUtil.show(error);
    });
  }

  var amount = 0.0.obs;
  int clickIndex = 0;
  var psw = ''.obs;

  _wirhdraw() {
    DioManager.getInstance().post(ServiceUrl.getAddUserWithdraw,{
      "amount":amount.value.toString(),
      "withdraw_type":clickIndex==0?'3':'2',
      "password":psw.value,
      'user_name':UserUtil.getUSER()!.userName
    }, (data1) async {
      SimpleEntity  entity = SimpleEntity.fromJson(jsonDecode(data1));

      ToastUtil.show(entity.msg);
    }, (error) {
      ToastUtil.show(error);
    });
  }

}

class WithdrawPage   extends GetView<WithdrawController>  {

  late WithdrawController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {
    Get.lazyPut<WithdrawController>(() => WithdrawController());
    controller = Get.find();
    context = context1;

    return GetBuilder<WithdrawController>(
      builder: (_){
        return  Scaffold(
            appBar: MyAppBar(context: context, title: '提现',hasLeading: true,),
            backgroundColor: Color(0xFFF7F8FC),
            body: _content());
      },
    );
  }



  Widget _content(){
    return SingleChildScrollView(
      child: Container(
        padding: EdgeInsets.only(left: 20.w,right: 27.w),
        child: Column(
          children: [
            SizedBox(height: 24.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text('提现金额',style: TextStyle(color: Color(0xFF333333),fontSize: 15.sp),)
              ],
            )
            ,
            Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6.r),
                  color: Color(0xFFF9F9F9)
              ),
              height: 48.h,
              margin: EdgeInsets.only(
                top: 15.h,
              ),
              child: MoneyTextField(
                //获取返回的输入内容
                hitText: '请输入金额',
                inputColor: 0xFFF9F9F9,
                contentStrCallBack: (content) {
                  controller.amount.value = double.parse(content)  ;
                }, key: ValueKey('money'),
              ),
            ),
            SizedBox(height: 27.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text('提现密码',style: TextStyle(color: Color(0xFF333333),fontSize: 15.sp),)
              ],
            )
            ,
            Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6.r),
                  color: Color(0xFFF9F9F9)
              ),
              height: 48.h,
              margin: EdgeInsets.only(
                top: 15.h,
              ),

                // child: Obx(() =>
                //     Text(
                //       controller.timeTip.value,
                //       style:
                //       TextStyle(color: Colors.white, fontSize: 11.sp),
                //     ),
                // ))

              child: AccountEdit2Text(
                //获取返回的输入内容
                hitText: '请输入',
                inputColor: 0xFFF9F9F9,
                contentStrCallBack: (content) {
                  controller.psw.value = content ;
                }, key: ValueKey('money'),
              ),
            ),

            SizedBox(height: 28.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Text('提现至'+(controller.clickIndex==0?'银行卡':'支付宝'),style: TextStyle(color: Color(0xFF333333),fontSize: 15.sp),)
              ],
            )
            ,
            Container(
              margin: EdgeInsets.only(top: 14.h),
              padding: EdgeInsets.only(left: 20.w,right: 22.w),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6.r),
                  color: Color(0xFFF9F9F9)
              ),
              child: Column(
                children: [
                  // _item(Constant.IMG_BASE+'icon_wx.png','微信',0),
                  _item(Constant.IMG_BASE+'icon_balacnce.png','银行卡',0),
                  _item(Constant.IMG_BASE+'icon_zfb.png','支付宝',1),
                  SizedBox(height: 36.h,)
                ],
              ),
            ),
            _btn()

          ],
        ),
      )
    ) ;
  }

  Widget _item(img,name,index){
    return Container(
      margin: EdgeInsets.only(top: 23.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Image.asset(img,width: 34.w,height: 34.h,),
              SizedBox(width: 12.w,),
              Text(name,style: TextStyle(color: Color(0xFF333333),fontSize: 14.sp,fontWeight: FontWeight.bold),),
            ],
          ),
          InkWell(
            onTap: (){
              controller.clickIndex=index;
              controller.update();
            },
            child:    controller.clickIndex==index?
            Image.asset(Constant.IMG_BASE+'radio_y.png',width: 14.w,height: 14.h,)
                :
            Image.asset(Constant.IMG_BASE+'radio_n.png',width: 14.w,height: 14.h,)
          )

        ],
      ),
    );
  }

  Future<Future<int?>> _showSingle() async {
    return showModalBottomSheet<int>(
        backgroundColor: Colors.transparent,
        isScrollControlled: true,
        context: context,
        builder: (BuildContext context1) {
          return AnimatedPadding(
              padding: MediaQuery.of(context1).viewInsets, // 我们可以根据这个获取需要的padding
              duration: const Duration(milliseconds: 100),
              child:  Container(
                  height: 200.h,
                  clipBehavior: Clip.antiAlias,
                  decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(20.0),
                      topRight: Radius.circular(20.0),
                    ),
                  ),
                  child: Column(
                    children: [
                      SizedBox(height: 15.h,),
                      Text('请输入密码',style: TextStyle(
                          fontSize: 15.sp
                      ),),
                      Container(
                          margin: EdgeInsets.all(10.w),
                          decoration: BoxDecoration(
                              border: Border.all(color: Color(0xFFE4E4E4),width: 1.w)
                          ),
                          child:
                          AccountEdit2Text(key: const ValueKey('title'),hitText: '请输入密码', contentStrCallBack: (value){
                            controller.psw.value = value;
                          })

                      ),
                      InkWell(
                          onTap: (){
                            Navigator.pop(context1);
                          },
                          child:  Container(
                            height: 40.h,
                            margin: EdgeInsets.all(10.w),
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(8.r),
                                color: const Color(0xFF007BE5)
                            ),
                            child: Text('确定',style: TextStyle(
                                color: Colors.white,
                                fontSize: 13.sp
                            ),),
                          )
                      )

                    ],
                  )
              )
          ) ;
        });
  }

  Widget _btn(){
    return InkWell(
        onTap: (){
          if(controller.amount.value == 0){
            ToastUtil.show('请输入金额');
            return;
          }
          if(controller.isPsw && controller.psw.value.isEmpty){
            ToastUtil.show('请输入密码');
            return;
          }else{
          }
          controller._wirhdraw();
        },
        child:Container(
          margin: EdgeInsets.only(top: 30.h),
          height: 42.h,
          width: MediaQuery.of(context).size.width - 64.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: Color(0xFF007BE5),
              borderRadius: BorderRadius.circular(22.r)
          ),
          child: Text('确定',style: TextStyle(
              fontSize: 14.sp,
              color: Colors.white
          ),),
        )
    );
  }

}
